package com.lzq.config;

/**
 * @author 2019/1/31 11:27 by 刘赵强
 **/

import com.alibaba.fastjson.JSONObject;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

public class RequestJsonHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.hasParameterAnnotation(RequestJson.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        RequestJson requestJson = parameter.getParameterAnnotation(RequestJson.class);
        String value = requestJson.value();
        String defaultvalue = requestJson.defaultvalue();
        Class clazz = parameter.getParameterType();
        String jsonData = webRequest.getParameter(value);
        if (jsonData == null && defaultvalue==null) {
            return null;
        }else if(jsonData == null && defaultvalue!=null){
            jsonData = defaultvalue;
        }
        Object object = JSONObject.parseObject(jsonData, clazz);
        return object;
    }
}
